package com.tanhua.admin.service;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentAdminVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/12/11 23:24
 */
@Service
public class CommentService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     * 动态列表
     * @param page
     * @param pagesize
     * @param messageID
     * @return
     */
    public PageResult pageFindComments(Integer page, Integer pagesize, String messageID) {

        List<Comment> commentList = commentApi.pageFindComments(page, pagesize, messageID);
        List<Long> userIds = CollectionUtil.getFieldValues(commentList, "userId", Long.class);

        if (CollectionUtil.isEmpty(commentList)){
            return new PageResult();
        }
        List<CommentAdminVo> vos = new ArrayList<>();
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);

        for (Comment comment : commentList) {
            UserInfo userInfo = map.get(comment.getUserId());
            if(userInfo != null){
                CommentAdminVo vo = CommentAdminVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0l, vos);

    }
}
